using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;
using SiteECSWeb;

/// <summary>
/// Summary description for RoleBiz
/// </summary>
namespace SiteECSWeb
{
    [DataObject(true)]
    public class RoleBiz
    {
        public RoleBiz()
        {

        }

        #region SelectRoles
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        static public List<RoleInfo> GetRoles()
        {
            return GetRoles(null, false);
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        static public List<RoleInfo> GetRoles(string userName,
            bool showOnlyAssignedRolls)
        {
            List<RoleInfo> roleList = new List<RoleInfo>();
            string[] rolsListStr = Roles.GetAllRoles();
            foreach (string roleName in rolsListStr)
            {
                bool userInRole = false;
                if (!string.IsNullOrEmpty(userName))
                {
                    userInRole = Roles.IsUserInRole(userName, roleName);
                }
                if (showOnlyAssignedRolls == false || userInRole == true)
                {
                    string[] usersInRole = Roles.GetUsersInRole(roleName);
                    RoleInfo rInfo = new RoleInfo();
                    rInfo.RoleName = roleName;
                    rInfo.UserName = userName;
                    rInfo.UserInRole = userInRole;
                    rInfo.NumberOfUsersInRole = usersInRole.Length;
                    roleList.Add(rInfo);
                }
            }
            return roleList;
        }
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        static public List<RoleInfo> GetAllRoles()
        {
            List<RoleInfo> roleList = new List<RoleInfo>();
            string[] rolsListStr = Roles.GetAllRoles();
            foreach (string roleName in rolsListStr)
            {
                RoleInfo rInfo = new RoleInfo();
                rInfo.RoleName = roleName;
                roleList.Add(rInfo);
            }
            return roleList;
        }
        #endregion

        #region InsertRoles
        [DataObjectMethod(DataObjectMethodType.Insert, true)]
        static public void Insert(string roleName)
        {
            if (!Roles.RoleExists(roleName))
            {
                Roles.CreateRole(roleName);
            }
        }
        #endregion

        #region UpdateRoles

        //no update for roles; may be in next edition 2008
        #endregion

        #region DeleteRoles
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        static public void Delete(string roleName)
        {
            MembershipUserCollection muc = Membership.GetAllUsers();
            string[] allUserNames = new string[1];

            foreach (MembershipUser mUser in muc)
            {
                if (Roles.IsUserInRole(mUser.UserName, roleName))
                {
                    allUserNames[0] = mUser.UserName;
                    Roles.RemoveUsersFromRole(allUserNames, roleName);
                }
            }
            Roles.DeleteRole(roleName);
        }

        #endregion

    }
}
